import { prisma } from '../../utils/prisma'
import { hashPassword } from '../../utils/auth'
import { z } from 'zod'

const bodySchema = z.object({
  phone: z.string().min(6),
  password: z.string().min(6)
})

export default eventHandler(async (event) => {
  const body = await readBody(event)
  const parsed = bodySchema.safeParse(body)
  if (!parsed.success) {
    throw createError({ statusCode: 422, statusMessage: 'INVALID_INPUT' })
  }
  const { phone, password } = parsed.data
  const exists = await prisma.user.findUnique({ where: { phone } })
  if (exists) throw createError({ statusCode: 409, statusMessage: 'PHONE_EXISTS' })

  const passwordHash = await hashPassword(password)
  await prisma.user.create({ data: { phone, passwordHash } })
  return { ok: true }
})


